package com.prj.ufdm.bsplatform.bpo._impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.prj.ufdm.auth.bpo.SysFunctionBPO;
import com.prj.ufdm.auth.model.SysFunction;
import com.prj.ufdm.bsplatform.bpo.BspfFunctionBPO;

/**  
 * 标准化平台菜单 
 * @author 胡义振  
 * @date 2018年3月26日  
*/
@Service 
public class BspfFunctionBPOImpl implements BspfFunctionBPO {
	
	@Value("${client.server.url:}")
	private String clientServerUrl;
	@Autowired
	SysFunctionBPO sysFunctionBPO;
	
    /**
     * Desc: 功能列表转为JSONObject 
     * @param listSysFunctionList
     * @return
     * @throws Exception
     * Date: 2020年1月14日
     * Author: Administrator
     */
    @Override
    public JSONArray doGetBspfFunctionJson(String userId) throws Exception {
    	
    	JSONArray jsonArray = new JSONArray();
    	
    	List<SysFunction> listSysFunction = sysFunctionBPO.doGetFunctionListByUserId(userId);
    	
    	System.out.println("\n listSysFunction:"+listSysFunction);
    	
		List<SysFunction> parentSysFunctionList = null;
		
		if(listSysFunction!=null) {
			// 获取根节点
			parentSysFunctionList = listSysFunction.stream().filter( t -> t.getParentId().equals("0")).collect(Collectors.toList());
			// 添加每个根节点所有节点
			parentSysFunctionList.stream().forEach(t -> {
				try {
					JSONObject treeObject = new JSONObject(true);
					JSONArray childrenArray = getChildrenMenuJSONObject(listSysFunction,t.getId());
					boolean isLeaf = (childrenArray!=null && childrenArray.size()>0) ? false : true;
					treeObject.put("jsid", t.getId());
					treeObject.put("leaf", isLeaf);
					treeObject.put("link", clientServerUrl + "/#" + t.getFunctionUrl());
					treeObject.put("orderno", t.getFunctionOrderNum());
					treeObject.put("text", t.getFunctionName());
					if(!isLeaf) {
					    treeObject.put("children", childrenArray);
					}
					jsonArray.add(treeObject);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			});
		}
		return jsonArray;
    }

    /**
     * Desc: 递归获取子节点
     * @param listSysFunctionList
     * @param parentId
     * @return
     * @throws Exception
     * Date: 2020年1月14日
     * Author: Administrator
     */
	private JSONArray getChildrenMenuJSONObject(List<SysFunction> listSysFunction,String parentId) throws Exception  {
		JSONArray jsonArray = new JSONArray();
		List<SysFunction> childrenFunctionList = null;
		if(listSysFunction!=null) {
			// 子节点列表
			childrenFunctionList = listSysFunction.stream().filter( t -> t.getParentId().equals(parentId))
					.filter(t -> t.getMenuFlag().equals("1")).collect(Collectors.toList());
			// 遍历子节点
			if(childrenFunctionList!=null) {
				childrenFunctionList.stream().forEach(t -> {
					try {
						JSONObject treeObject = new JSONObject(true);
						JSONArray childrenArray = getChildrenMenuJSONObject(listSysFunction,t.getId());
						boolean isLeaf = (childrenArray!=null && childrenArray.size()>0) ? false : true;
						treeObject.put("jsid", t.getId());
						treeObject.put("leaf", isLeaf);
						treeObject.put("link", clientServerUrl + "/#" + t.getFunctionUrl());
						treeObject.put("orderno", t.getFunctionOrderNum());
						treeObject.put("text", t.getFunctionName());
						if(!isLeaf) {
						    treeObject.put("children", childrenArray);
						}
						jsonArray.add(treeObject);
					} catch (Exception e) {
						e.printStackTrace();
					}
				});
			}
		}
		return jsonArray;
	}
}
